package student_System;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class app {
    public static void main(String[] args) {
        //创建一个集合来收user
        ArrayList<user> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("----------欢迎来到学生管理系统----------");
            System.out.println("请选择以下操作：");
            System.out.println("1.登录  2.注册  3.忘记密码  4.退出");
            System.out.println("请输入您的选择：");
            String choose = sc.next();
            switch (choose) {
                case "1" -> {
                    System.out.println("登录：");
                    SignIn(list);
                }
                case "2" -> {
                    System.out.println("注册：");
                    register(list);
                }
                case "3" -> {
                    System.out.println("忘记密码");
                    forgetPasswd(list);
                }
                case "4" -> {
                    System.out.println("退出");
                    //退出当前的循环
                    System.exit(0);
                }
                default  -> System.out.println("抱歉，没有这个选项");
            }
        }
    }
    //忘记密码的方法
    private static void forgetPasswd(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            //键盘输入用户名
            System.out.println("请输入您的用户名");
            String userName = sc.next();
            boolean falg = contins(list, userName);
            int index = getIndex(list, userName);
            if (!falg) {
                System.out.println(userName + "未注册");
                //结束方法
                return;
            }
            System.out.println("请输入您的身份证号：");
            String personId = sc.next();
            if ( !(list.get(index).getPersonId().equals(personId) ) ) {
                System.out.println("您输入的身份证错误，无法修改密码");
                return;
            }
            System.out.println("请输入您的手机号：");
            String phoneId = sc.next();
            if ( !(list.get(index).getPhoneId().equals(phoneId) ) ) {
                System.out.println("您输入的手机号错误，无法修改密码");
                return;

            }
            System.out.println("请输入您要修改的密码：");
            String newPasswd = sc.next();
            System.out.println("再次确认您的密码：");
            String newPasswd1 = sc.next();
            if (!newPasswd.equals(newPasswd1)) {
                System.out.println("两次密码不相同。修改密码失败");
                return;
            }else {
                list.get(index).setPasswd(newPasswd);
                System.out.println("修改密码成功。");
                return;
            }


        }

    }

    //注册的方法
    private static void register(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        String userName;
        String passwd;
        String personId;
        String phoneId;
        //用户名判断
        while(true) {
            System.out.println("请输入用户名：");
            userName = sc.next();
            boolean flag1 = contins(list, userName);
            boolean flag2 = pdUserName(list , userName);
            if (flag1) {
                System.out.println("用户名为" + userName + "的已经注册过了");
                return;
            }
            if (flag2) {
                System.out.println("用户名正确");
                break;
            }
        }
        //密码判断
        while(true) {
            System.out.println("请输入密码：");
            passwd = sc.next();
            System.out.println("请输入密码：");
            String isPasswd = sc.next();
            if (!passwd.equals(isPasswd)) {
                System.out.println("两次密码不相同，请重新输入");
            }else {
                System.out.println("密码正确");
                break;
            }
        }
        //身份证验证
        while (true) {
            System.out.println("请输入身份证号：");
            personId = sc.next();
            boolean flag = isPersonId(personId);
            if (flag) {
                System.out.println("身份证号正确");
                break;
            }else {
                System.out.println("身份证号错误，请重新输入");
            }
        }
        //手机号的验证
        while (true) {
            System.out.println("请输入手机号：");
            phoneId = sc.next();
            boolean flag = isPhoneId(phoneId);
            if (flag) {
                System.out.println("手机号正确，注册成功");
                break;
            } else {
                System.out.println("手机号输入错误，请重新输入");
            }
        }
        //顺序要对，一定要对
        user c = new user(userName , passwd ,phoneId  , personId);
        list.add(c);
        //遍历
        for (int i = 0; i < list.size(); i++) {
            user u = list.get(i);
            System.out.print(u.getUserName() + "  ");
            System.out.print(u.getPasswd() + "  ");
            System.out.print(u.getPersonId() + "  ");
            System.out.println(u.getPhoneId() + "  ");
        }

    }

    //登录的方法
    private static void SignIn(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        String userName;
        //计数器
        int count = 0;
        int count1 = 0;
        loop: while (true) {
            //1.键盘录入用户名
            System.out.println("请输入用户名：");
            userName = sc.next();
            boolean flag1 = contins(list,userName);
            if (!flag1) {
                System.out.println(userName + "还没有注册，请先注册后才能登录");
                break;
            }else {
                System.out.println("用户名正确");
                while (true) {
                    //2.键盘录入密码
                    System.out.println("请输入密码：");
                    String passwd = sc.next();
                    boolean flag2 = contins1(list,passwd);
                    if (!flag2) {
                        System.out.println("密码错误，请重新输入");
                        count++;
                        if (count == 3) {
                            System.out.println("您的账号：" + userName + "已经被锁定，请联系客服1234-5678解决");
                            break loop;
                        }else {
                            System.out.println("您还有" + (3-count) + "次机会");
                        }
                    }else {
                        System.out.println("密码正确");
                        System.out.println();
                        while (true) {
                            String code = getCode();
                            System.out.println("正确的验证码为：" + code);
                            System.out.println("请输入正确的验证码：");
                            String scCode = sc.next();
                            if (scCode.equalsIgnoreCase(code)) {
                                System.out.println("验证码正确");
                                System.out.println("登录成功");
                                studentSystem user = new studentSystem();
                                user.studentSystemIN();
                                break loop;
                            }else {
                                System.out.println("验证码错误，请重新输入");
                                count1++;
                                if (count1 == 3) {
                                    System.out.println("您输入验证码的次数用完了，请重新登录");
                                    break loop;
                                }else {
                                    System.out.println("您还有" + (3-count) + "次机会");
                                }
                            }
                        }
                    }
                }
                //获取验证码

            }
        }



        //3.获取一个验证码
        //4.键盘录入验证码

    }
    //获取验证码
    private static String getCode() {
        Random r =  new Random();
        StringBuffer sb = new StringBuffer();
        //创建一个数组
        char[] arr = new char[52];
        for (int i = 0; i < arr.length; i++) {
            if ( i <= 25){
                arr[i] = (char)('a' + i);
                arr[i+26] = (char)('A' + i);
            }
        }
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(52);
            sb.append(arr[index]);
        }
        sb.append(r.nextInt(10));
        String sb1 = sb.toString();
        char[] arrsb = sb1.toCharArray();
        int index = r.nextInt(5);
        char temp1 = arrsb[index];
        char temp2 = arrsb[arrsb.length - 1 ];
        arrsb[index] = temp2;
        arrsb[arrsb.length - 1] = temp1;

        return new String(arrsb);
    }

    //手机号是否合法
    private static boolean isPhoneId(String phoneId) {
        if (phoneId.length() != 11) {
            System.out.println(1);
            return false;
        }
        char[] arr = phoneId.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if ( !( arr[i] >= '0' && arr[i] <= '9' ) ) {
                System.out.println(2);
                return false;
            }
        }
        return true;
    }

    //判断身份证号是否合法
    private static boolean isPersonId(String personId) {
        char[] arr  = personId.toCharArray();
        //调用字符串里面的length()方法获取长度，只有为18的时候才满足
        if (personId.length() != 18) {
            System.out.println("1");
            return false;
        }
        //可以调用charAt(0)获取第-个字符，跟'0'进行比较
        if (personId.charAt(0) == '0') {
            System.out.println("2");
            return false;
        }
        //遍历前面17位，如果有一个不在'0'~'9' 这个范围当中，那么就是不符合的
        for (int i = 0; i < arr.length - 1; i++) {
            if (!(arr[i] >= '0' && arr[i] <= '9')) {
                System.out.println("3");
                return false;
            }
        }
        //最后一个字符在'0' ~'9' 之间 或者'X' 或者'x'
        char last = personId.charAt(personId.length() - 1);
        if ( !( (last >= '0' && last <= '9') || (last == 'x') || (last == 'X') ) ) {
            System.out.println("4");
            return false;
        }

        return true;
    }

    //判断用户名是否正确
    private static boolean pdUserName( ArrayList<user> list ,String userName) {
        for (int i = 0; i < list.size(); i++) {
            user c = list.get(i);
            if (c.getUserName().equals(userName)) {
                return false;
            }
        }
        //.长度必须再3~15之间。可以.用字符串的length()
        int len = userName.length();
        if (!(len >=3 && len <=15)) {
            System.out.println("用户名填写错误，请重新输入");
            return false;
        }
        //3.先循环获取username里面的每一个字符
        //'A'~'Z’'0’~’9'，那么当前用户名不符合条件如果有一个字符不在'a'~'z统计一下用户
        // 名username当中有多少个字符在a-z A-Z判断一个个数，
        // 只有大于等于0才是满足要求的用户名
        char[] arr = userName.toCharArray();
        //计数器
        int count = 0;
        for (int i = 0; i < 26; i++) {
            if ( ( arr[i] >= ('a' + i ) && arr[i] <= ('z'  +i ) ) || ( ( arr[i] >= ('A' + i ) && arr[i] <= ('Z'  +i ) ))) {
                count++;
                break;
            }
        }
        if (count <= 0) {
            return false;
        }

        return true;
    }

    //判断集合中有没有这个用户名
    private static boolean contins(ArrayList<user> list,String id) {
        //如果大于等于0则说明重复了，若返回-1则表明在list里面找不到相同的id，则返回false
        return getIndex(list, id) >=0;
    }

    //判断集合中有没有这个密码
    private static boolean contins1(ArrayList<user> list,String passwd) {
        //如果大于等于0则说明重复了，若返回-1则表明在list里面找不到相同的id，则返回false
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //的到每一个学生对象
            user stu = list.get(i);
            //获取每一个学生对象的id
            String sid = stu.getPasswd();
            if (sid.equals(passwd)) {
                //如果id相同就返回对应的索引
                return true;
            }
        }
        return false;
    }
    //获取用户名对应的索引
    public static int getIndex(ArrayList<user> list,String id) {
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //获取集合里的到每一个用户
            user stu = list.get(i);
            //获取每一个学生对象的用户名
            String sid = stu.getUserName();
            if (sid.equals(id)) {
                //如果用户名相同就返回对应的索引
                return i;
            }
        }
        //否则就返回-1
        return -1;
    }

}
